package com.sdyd.antivirus.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sdyd.antivirus.dao.GroupInfoMapper;
import com.sdyd.antivirus.model.GroupInfo;
import com.sdyd.antivirus.service.HealthCollectionService;
import com.sdyd.antivirus.utils.AesEncryptUtil;
import com.sdyd.antivirus.utils.UUIDUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author:
 * @Date:
 * 身体状况采集
 */
@Service(value = "HealthCollectionService")
public class HealthCollectionServiceImpl implements HealthCollectionService {

    private Logger logger= LoggerFactory.getLogger(HealthCollectionServiceImpl.class);
    @Autowired
    private GroupInfoMapper groupInfoMapper;


    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public String addGroupData(GroupInfo group_info)  throws Exception{

        JSONObject obj = new JSONObject();
        try{
            //解密手机号
            AesEncryptUtil.decrypt(group_info.getPhoneNumber());
            //解密邮箱
            AesEncryptUtil.decrypt(group_info.getEmail());


            //获取当天时间
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date();
            String curTime = formatter.format(date);

            group_info.setId(UUIDUtils.getUUID());//生成UUID
            group_info.setAddTime(curTime);
            group_info.setStatus(2);//0已反馈，1正在处理，2尚未处理

            obj.put("success",true);
            GroupInfo exitGroupInfo = groupInfoMapper.queryExitGroupInfo(group_info);
            if (exitGroupInfo == null){//单位不存在插入
                groupInfoMapper.addGroupData(group_info);
            }else{//保留status状态且更新其他信息
    //            group_info.setStatus(exitGroupInfo.getStatus());//0已反馈，1正在处理，2尚未处理
    //            groupInfoMapper.updateByNameEmail(group_info);
                obj.put("success",false);
                obj.put("msg","贵单位已申请，谢谢使用");
            }

        }catch (Exception e){
            e.printStackTrace();
            obj.put("success",false);
            obj.put("msg","提交失败，请联系管理员，谢谢使用");
        }

        return obj.toString();
    }



}
